package com.vay.de.dang.online.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.vay.de.dang.online.ui.BaseActivity;
import com.vay.de.dang.online.ui.apply.ConfirmLoanActivity;
import com.vay.de.dang.online.web.LoanApi;
import com.vay.de.dang.online.web.ReqResult;
import com.vay.de.dang.online.resp.RFinancialProductDto;
import com.vay.de.dang.online.web.ReqCallback;
//import com.vay.de.dang.online.ui.apply.FinancialProduct1Activity;
import com.vay.de.dang.online.ui.sign.SignInActivity;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

public final class VnUIManager {

    private static Stack<BaseActivity> mStacks = new Stack<>();

    public static void addActivity(BaseActivity activity) {
        mStacks.add(activity);
    }

    public static void closeActivity(BaseActivity activity) {
        for (BaseActivity act : mStacks) {
            if (act == activity) {
                mStacks.remove(act);
                break;
            }
        }
        activity.finish();
    }

    public static void closeAll() {
        for (BaseActivity activity : mStacks) {
            activity.finish();
        }
        mStacks.clear();
    }

    public static void nextActivityWithLogin(Context context, Class<?> cls) {
        if (VnContext.instance().isLogin()) {
            nextActivity(context, cls, null, false);
        } else {
            nextActivity(context, SignInActivity.class, null, false);
        }
    }

    public static void nextActivity(Context context, Class<?> cls) {
        nextActivity(context, cls, null, false);
    }

    public static void nextActivity(Context context, Class<?> cls, boolean finish) {
        nextActivity(context, cls, null, finish);
    }

    public static void nextActivity(Context context, Class<?> cls, Map<String, Object> map) {
        nextActivity(context, cls, map, false);
    }

    public static void nextActivity(Context context, Class<?> cls,
                                    Map<String, Object> map, boolean finish) {
        Intent next = new Intent(context, cls);
        if (map != null) {
            Set<String> keys = map.keySet();
            for (String key : keys) {
                Object value = map.get(key);
                if (value instanceof String) {
                    map.put(key, value.toString());
                } else if (value instanceof Integer) {
                    map.put(key, (Integer) value);
                } else if (value instanceof Long) {
                    map.put(key, (Long) value);
                } else if (value instanceof Double) {
                    map.put(key, (Double) value);
                } else if (value instanceof Float) {
                    map.put(key, (Float) value);
                } else if (value instanceof Serializable) {
                    map.put(key, (Serializable) value);
                }
            }
        }
        context.startActivity(next);
        if (finish) {
            if (context instanceof BaseActivity) {
                ((BaseActivity) context).finish();
            }
        }
    }

    // 金融产品选择(只有单个直接进入贷款确认页面，多个进入选择页面)
    public static void nextFinancialProduct(Activity activity, boolean finishAll) {
        LoanApi.queryApplyProduct(new ReqCallback(activity) {

            @Override
            public void callback(ReqResult value) {
                if (value.isOk()) {
                    List<RFinancialProductDto> list = (List<RFinancialProductDto>) value.result;
                    if (list != null) {
                        RequestHelper.setFinancialProducts(list);
                        nextActivity(activity, ConfirmLoanActivity.class);
//                        if (list.size() == 1) {
//                            RequestHelper.setCurrentProduct(list.get(0));
//                            nextActivity(activity, ConfirmLoanActivity.class);
//                        } else {
//                            RequestHelper.setFinancialProducts(list);
//                            nextActivity(activity, FinancialProduct1Activity.class);
//                        }
                    }
                }
            }

            @Override
            public void error(ReqResult error) {
                if (finishAll) {
                    VnUIManager.closeAll();
                }
            }

        }.showDialog(true));
    }
}
